﻿//==========================================================
//Đã sửa bởi tranninh_86_yb@yahoo.com ngày 23/09/2013
//==========================================================

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

using DevExpress.XtraEditors;
using prjKQXS_UI.Hop_dong;
using prjKQXS_DLL;

namespace prjKQXS_UI.View
{
    public partial class frmDSLoaiHopDong : DevExpress.XtraEditors.XtraForm
    {
        public frmDSLoaiHopDong()
        {
            InitializeComponent();
        }

        frmDMHopDong frmDMHopDong = null;
        public frmDSLoaiHopDong(frmDMHopDong frmHOPDONG)
        {
            InitializeComponent();
            this.frmDMHopDong = frmHOPDONG;
        }

        SqlConnection conn = new SqlConnection(BienTc.connString);


        private void loadDAta(bool isPublic)
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                SqlDataAdapter da = new SqlDataAdapter("Select * from DMLOAIHOPDONG Where isPublic=@isPublic", conn);
                da.SelectCommand.Parameters.AddWithValue("@isPublic", isPublic);
                DataTable dt = new DataTable();
                da.Fill(dt);

                gcLoaiHD.DataSource = dt;
            }
            catch (Exception)
            {
                clsCommon.MsgBox("Không thể kết nối tới máy chủ", 0);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        private void rpURL_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {            
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                clsCommon.updateRowValue("DMLOAIHOPDONG", "URL", "Where ID=" + gvLoaiHD.GetFocusedDataRow()["ID"].ToString(), openFileDialog1.FileName);
                loadDAta(isPublic);
            }
        }

        private void frmDSLoaiHopDong_Load(object sender, EventArgs e)
        {
            loadDAta(isPublic);
        }

        private void btnThemmoi_Click(object sender, EventArgs e)
        {                                            
            try
            {                
                if (chkKhoiPhuc.Checked == true)
                {
                    if (gvLoaiHD.RowCount > 0)
                    {
                        clsCommon.deleteRestoreRow("DMLOAIHOPDONG", "ID", gvLoaiHD.GetFocusedDataRow()["ID"].ToString(), true, false);
                        loadDAta(isPublic);
                    }
                }
                else
                {
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    string madt = clsCommon.laymadt("DMLOAIHOPDONG", "Ma_DT", 5);
                    SqlCommand cmd = new SqlCommand("Insert into DMLOAIHOPDONG(Ma_DT,Ten_DT) values(@Ma_dt,@Ten_dt)", conn);
                    cmd.Parameters.AddWithValue("@Ma_dt", madt);
                    cmd.Parameters.AddWithValue("@Ten_dt", "Đối tượng thêm mới");
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        loadDAta(isPublic);
                    }
                    gvLoaiHD.FocusedRowHandle = ((DataTable)gcLoaiHD.DataSource).Rows.Count - 1;
                }
            }
            catch (Exception)
            {
                clsCommon.MsgBox("Không thể kết nối tới máy chủ", 0);
            }            
        }

        private void gvLoaiHD_ColumnChanged(object sender, EventArgs e)
        {
           
        }

        private void gvLoaiHD_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (gvLoaiHD.FocusedColumn.FieldName != "URL")
            {
                clsCommon.updateRowValue("DMLOAIHOPDONG", e.Column.FieldName, "Where ID=" + gvLoaiHD.GetFocusedDataRow()["ID"].ToString(), e.Value);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvLoaiHD.RowCount > 0)
                {
                    if (chkKhoiPhuc.Checked == false)
                    {
                        clsCommon.deleteRestoreRow("DMLOAIHOPDONG", "ID", gvLoaiHD.GetFocusedDataRow()["ID"].ToString(), false, false);
                    }
                    else
                    {
                        clsCommon.deleteRestoreRow("DMLOAIHOPDONG", "ID", gvLoaiHD.GetFocusedDataRow()["ID"].ToString(), false, true);
                    }
                    loadDAta(isPublic);
                }
            }
            catch (Exception)
            {
                clsCommon.MsgBox("Không kết nối được máy chủ !", 0);
            } 
        }

        private bool isPublic = true;
        private void chkKhoiPhuc_CheckedChanged(object sender, EventArgs e)
        {
            if (chkKhoiPhuc.Checked == true)
            {
                btnThemmoi.Text = "&Khôi phục";
                btnThemmoi.ImageIndex = 2;
                isPublic = false;
                loadDAta(isPublic);
            }
            else
            {
                btnThemmoi.Text = "&Thêm mới";
                btnThemmoi.ImageIndex = 7;
                isPublic = true;
                loadDAta(isPublic);
            }
            
        }
        private void btnChon_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmDSLoaiHopDong_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frmDMHopDong != null)
            {
                if (gvLoaiHD.RowCount > 0)
                {
                    clsCommon.updateRowValue("HOPDONG", "Ma_DMLoaiHD", "Where ID=" + frmDMHopDong.id, gvLoaiHD.GetFocusedDataRow()["Ma_DT"].ToString());
                }
            }
        }
    }
}